package cn.kys.modules.system.repository;

import cn.kys.modules.system.pojo.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


public interface TaskMapper {

    /**
     * 查询全部部门
     *
     * @return
     */
    @Select("SELECT * FROM t_task")
    List<Map> list();


    List<Map> listContidon(@Param("name") String name, @Param("casename") String casename, @Param("isPublic") boolean isPublic);

    @Select(" SELECT count(*) FROM t_subtask_geom WHERE taskid=#{taskid} AND check_status IS NULL OR check_status!='已提交'")
    Long countUncommit(@Param("taskid") Integer taskid);

    @Select("SELECT *,ST_AsText(geom) FROM  ${layername} WHERE ST_Disjoint((SELECT ST_Union(geom) FROM public.t_subtask_geom WHERE taskid=#{taskid}),geom)")
    List<Map> showUnBindPolygonList(@Param("layername") String layername, @Param("taskid") Integer taskid);

    @Select("SELECT *,ST_AsText(geom) FROM  ${layername}")
    List<Map> showUnBindPolygonWholeTable(@Param("layername") String layername);

    @Select("SELECT layername FROM public.t_case_editlayer WHERE caseid IN (SELECT caseid FROM t_task WHERE id =#{taskid})")
    String getLayerNameById(@Param("taskid") Integer taskid);

    @Select("SELECT id FROM t_subtask WHERE userid=#{userid} AND taskid=#{taskid}")
    Integer querySubTaskId(SubtaskDto subtaskDto);

    @Select("SELECT  st_astext(geom) AS  wkt FROM ${layername} WHERE gid=#{gid}")
    String queryGeomFromImportShpByGid(@Param("layername") String layername, @Param("gid") Integer gid);

    @Insert(" INSERT INTO public.t_subtask_geom(subtaskid, gid, layername, iscreated, geom, taskid, check_status) " +
            "VALUES (#{subtaskid}, #{gid}, #{layername}, #{iscreated}, ${geomStr}, #{taskid}, #{check_status})")
    Integer insertSubtaskGeom(SubtaskGeom subtaskGeom);


    @Select("SELECT * FROM t_subtask WHERE taskid=#{taskid}")
    List<SubtaskPojo> querySubTaskByTaskId(@Param("taskid") Integer taskid);

    @Select("SELECT *,ST_AsText(geom) AS geomStr  FROM t_subtask_geom WHERE taskid=#{taskid} AND  subtaskid=#{subtaskid}")
    List<SubtaskGeom> querySubtaskGeomBy2ID(@Param("taskid") Integer taskid, @Param("subtaskid") Integer subtaskid);

    @Select("SELECT EXISTS (SELECT 1 FROM t_subtask_geom WHERE taskid =#{taskid})")
    boolean taskIdExistSubtaskGeomTable(@Param("taskid") Integer taskid);


    @Insert("INSERT INTO public.t_subtask_polygon(subtaskid, polygon, geom, taskid) VALUES (#{subtaskid}, #{polygon}, ${wkt}, #{taskid})")
     void isertSubtaskPolygon(SubtaskPolygon subtaskPolygon);

    @Select("SELECT  * from t_task")
    List<Task> queryAllTaskId();

    @Select("SELECT  count(*) from t_subtask_geom WHERE taskid=#{taskid}")
    Integer querySubTaskCount(SubtaskGeom subtaskGeom);

    @Select("SELECT  count(*) from t_subtask_geom WHERE taskid=#{taskid} and check_status='已提交' ")
    Integer queryHasSummitTaskCount(SubtaskGeom subtaskGeom);

    @Select("SELECT * FROM t_task where dept_id=#{deptId}")
    List<Task> queryAllTaskIdWithDeptId(Long deptId);
}
